import { Service, Inject } from "typedi";
import { DynamicService } from "../DynamicService";
import { DynamicDao } from "../../dao/DynamicDao";
import { UserDao } from "../../dao/UserDao";
import { utils } from "../../utils/utils";
import { TopicServiceImpl } from "./TopicServiceImpl";
import {
  DynamicModel,
  ChangeDynamicLike,
  SearchTagModel,
  DynamicNewsModel,
  DynamicInfoModel,
  ChangeDynamicCollection,
  getRecentVisitUserDataModel,
} from "../../model/DynamicModel";
import { DynamicEntity } from "../../entity/DynamicEntity";
import { UserEntity } from "../../entity/UserEntity";
import { HomeDao } from "../../dao/HomeDao";
import { TagEntity } from "../../entity/TagEntity";

@Service()
export class DynamicServiceImpl implements DynamicService {
  @Inject()
  private DynamicDao!: DynamicDao;
  @Inject()
  private UserDao!: UserDao;
  @Inject()
  private HomeDao!: HomeDao;

  public async getDynamicData(params: DynamicModel): Promise<any> {
    let userInfo: UserEntity | undefined = await this.UserDao.getUserInfoDao(
      parseInt(params.user_Id)
    );
    let dynamic_List: DynamicEntity[] = [];
    let topic_List: TagEntity[] = [];
    let recentVisit_List: UserEntity[] = [];
    if (params.dynamic_status != "0") {
      await Promise.all(
        JSON.parse(userInfo!.user_UserInfo.user_FollowList).map(
          async (e: string, i: number) => {
            dynamic_List.push.apply(
              dynamic_List,
              await this.DynamicDao.getFollowDynamicDao(parseInt(e))
            );
          }
        )
      ).then((i) => {
        dynamic_List.sort((a: DynamicEntity, b: DynamicEntity): number => {
          return b.dynamic_Time.getTime() - a.dynamic_Time.getTime();
        });
      });
      await Promise.all(
        dynamic_List.map(async (e: DynamicEntity, dynamicIndex: number) => {
          return await this.changeDynamic(e, userInfo!.user_Id);
        })
      ).then((res: DynamicEntity[]) => {
        dynamic_List = res;
      });
      return {
        dynamic_List: dynamic_List.splice(
          parseInt(params.dynamic_Page) * parseInt(params.dynamic_Count),
          parseInt(params.dynamic_Count)
        ),
        topic_List,
        recentVisit_List,
      };
    } else {
      dynamic_List = await this.DynamicDao.getDynamicDao(
        parseInt(params.dynamic_Page),
        parseInt(params.dynamic_Count)
      );
      await Promise.all(
        dynamic_List.map(async (e: DynamicEntity, dynamicIndex: number) => {
          return await this.changeDynamic(e, userInfo!.user_Id);
        })
      ).then((res: DynamicEntity[]) => {
        dynamic_List = res;
      });
      topic_List = await this.DynamicDao.getDynamicTagDao(
        parseInt(params.tag_Page),
        parseInt(params.tag_Count)
      );
      await Promise.all(
        topic_List.map(async (e: any) => {
          return await new TopicServiceImpl().changeTopic(e, userInfo!);
        })
      ).then((res: TagEntity[]) => {
        topic_List = res;
      });
      let oldList: string[] = JSON.parse(
        userInfo!.user_UserInfo.user_RecentVisitUserList
      );
      let newList: string[] = oldList.splice(0, 100);
      await Promise.all(
        newList.map(async (e: string) => {
          return await this.DynamicDao.getRecentVisitDao(parseInt(e));
        })
      ).then((i: UserEntity[]) => {
        recentVisit_List = i;
      });
      return { dynamic_List, topic_List, recentVisit_List };
    }
  }

  public async changeDynamicLike(params: ChangeDynamicLike): Promise<any> {
    let userInfo: UserEntity | undefined = await this.UserDao.getUserInfoDao(
      parseInt(params.user_Id)
    );
    let DynamicLikeList: string[] = JSON.parse(
      userInfo!.user_UserInfo.user_DynamicLikeList
    );

    if (params.isLike == "true") {
      DynamicLikeList[DynamicLikeList.length] = params.dynamic_Id;
    } else {
      DynamicLikeList.forEach((element: string, i: number) => {
        if (element == params.dynamic_Id) {
          DynamicLikeList.splice(i, 1);
          return;
        }
      });
    }
    await this.DynamicDao.changeLikeDynamic(
      parseInt(params.dynamic_Id),
      params.isLike
    );
    return await this.UserDao.setDynamicLikeListDao(
      parseInt(params.user_Id),
      JSON.stringify(DynamicLikeList)
    );
  }
  public async ChangeDynamicCollection(
    params: ChangeDynamicCollection
  ): Promise<any> {
    let userInfo: UserEntity | undefined = await this.UserDao.getUserInfoDao(
      parseInt(params.user_Id)
    );
    let DynamicCollectionList: string[] = JSON.parse(
      userInfo!.user_UserInfo.user_DynamicCollectionList
    );

    if (params.isCollection == "true") {
      DynamicCollectionList[DynamicCollectionList.length] = params.dynamic_Id;
    } else {
      DynamicCollectionList.forEach((element: string, i: number) => {
        if (element == params.dynamic_Id) {
          DynamicCollectionList.splice(i, 1);
          return;
        }
      });
    }
    await this.DynamicDao.changeCollectionDynamic(
      parseInt(params.dynamic_Id),
      params.isCollection
    );
    return await this.UserDao.setDynamicCollectionListDao(
      parseInt(params.user_Id),
      JSON.stringify(DynamicCollectionList)
    );
  }

  public async searchTag(params: SearchTagModel): Promise<any> {
    let taglist = await this.DynamicDao.searchTag(params.search_Content);
    if (taglist.length == 0) {
      return "未找到相关话题";
    } else {
      return taglist;
    }
  }

  public async dynamicInfo(params: DynamicInfoModel): Promise<any> {
    return await this.changeDynamic(
      await this.DynamicDao.getOneDynamic(params.dynamic_Id),
      params.user_Id
    );
  }
  public async getRecentVisitUserData(
    params: getRecentVisitUserDataModel
  ): Promise<any> {
    let user: UserEntity | undefined = await this.UserDao.getUserInfoDao(
      parseInt(params.user_Id)
    );
    let userRecentVisitList: string[] = JSON.parse(
      user!.user_UserInfo.user_RecentVisitUserList
    ).splice(
      parseInt(params.Page) * parseInt(params.count),
      parseInt(params.Page) *  parseInt(params.count) + parseInt(params.count)
    );
    return await Promise.all(userRecentVisitList.map(async (e:string) => {

     return await this.UserDao.getUserInfoDao(parseInt(e))

    })).then(async (res:any[])=>{
      return await Promise.all(res.map(async (e:UserEntity) => {

        return await this.changeUser(e,user!)
   
       })).then((res:any[])=>{
        return res;
       });
    });
  }
  public async changeUser(user: UserEntity, myUser: UserEntity): Promise<any> {
 
    JSON.parse(myUser!.user_UserInfo.user_FollowList).some((e: string) => {
      if (parseInt(e) == user.user_Id) {
        user.isFollow = true;
        return true;
      }
    });
    user.user_NickName = utils.uncodeUtf16(user.user_NickName);
    if(user.user_UserInfo){
      if(user.user_UserInfo.user_Autograph)user.user_UserInfo.user_Autograph = utils.uncodeUtf16(user.user_UserInfo.user_Autograph);
    }
    return user;
  }
  public async dynamicNew(params: DynamicNewsModel): Promise<any> {
    if (typeof params.dynamic_ImgList == "string") {
      params.dynamic_ImgList = [params.dynamic_ImgList];
    }
    if (typeof params.dynamic_TagList == "string") {
      params.dynamic_TagList = [params.dynamic_TagList];
    }
    let dynamic_id: string = await this.DynamicDao.dynamicNew(params);
    if (params.dynamic_TagList) {
      await Promise.all(
        params.dynamic_TagList.map(async (e: string) => {
          let tag: TagEntity = await this.DynamicDao.getOneTag(e);
          let newList: string[];

          newList = JSON.parse(tag.tag_DynamicList);
          newList.unshift(dynamic_id);
          return await this.DynamicDao.setTagDynamic(
            JSON.stringify(newList),
            e
          );
        })
      );
    }
    await this.UserDao.changeDynamicNum(params.user_id, "true");
    return "执行成功";
  }

  public async changeDynamic(
    dynamic: DynamicEntity,
    userId: number
  ): Promise<DynamicEntity> {
    let userInfo: UserEntity | undefined = await this.UserDao.getUserInfoDao(
      userId
    );
    JSON.parse(userInfo!.user_UserInfo.user_FollowList).some((e: string) => {
      if (parseInt(e) == dynamic.dynamic_User.user_Id) {
        dynamic.dynamic_User.isFollow = true;
        return true;
      }
    });
    dynamic!.dynamic_User.user_NickName = utils.uncodeUtf16(
      dynamic!.dynamic_User.user_NickName
    );
    JSON.parse(userInfo!.user_UserInfo.user_DynamicCollectionList).some(
      (e: string) => {
        if (parseInt(e) == dynamic.dynamic_Id) {
          dynamic.isCollection = true;
          return true;
        }
      }
    );
    JSON.parse(userInfo!.user_UserInfo.user_DynamicLikeList).some(
      (e: string) => {
        if (parseInt(e) == dynamic.dynamic_Id) {
          dynamic.isLike = true;
          return true;
        }
      }
    );
    dynamic.dynamic_TagList = JSON.parse(dynamic.dynamic_TagList);
    dynamic.dynamic_Info.dynamicInfo_ImgList = JSON.parse(
      dynamic.dynamic_Info.dynamicInfo_ImgList
    );
    dynamic.dynamic_Introduce = utils.uncodeUtf16(dynamic.dynamic_Introduce);
        return dynamic;
  }
}
